package com.csz.service.impl;

import com.csz.Vo.UserVo;
import com.csz.mapper.UserMapper;
import com.csz.pojo.User;
import com.csz.pojo.UserExample;
import com.csz.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true) // 事务的回滚
    public User login(String uLoginname, String uPassword) {
        UserExample example = new UserExample();
        example.createCriteria().andULoginnameEqualTo(uLoginname).andUPasswordEqualTo(uPassword);
        List<User> users = userMapper.selectByExample(example);
        if (users.size()>0){
            return users.get(0);
        }else {
            return null;
        }
    }

    @Override
    public int updatePassword(Integer uId,String password) {
        UserExample example = new UserExample();
        example.createCriteria().andUIdEqualTo(uId);
        User user = userMapper.selectByPrimaryKey(uId);
        user.setuPassword(password);
        int i = userMapper.updateByExample(user, example);
        return i;
    }

    /**
     * 多条件分页查询
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    @Override
    public PageInfo<User> getUses(Integer pageNum, Integer pageSize, UserVo vo) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if (vo!=null){
            if(vo.getuTruename()!=null &&!"".equals(vo.getuTruename().trim())){
                criteria.andUTruenameLike("%"+vo.getuTruename().trim()+"%");
            }
        }
        //分页配置
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.selectByExample(example);
        return new PageInfo<>(users);

    }

    @Override
    public User findById(Integer uId) {
        return null;
    }
}
